package com.ykdl.member.kid.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.AnonymousBean;
import com.ykdl.member.kid.beans.RegisterPhonebean;
import com.ykdl.member.kid.gears.WelcomeActivity;
import com.ykdl.member.kid.prefs.MyPrefs;

/**
 * 该类用于保存RegisterPhonebean到sharepreference，并提供读取功能
 * 
 * @author chenshuai
 * 
 */
public class AccessTokenKeeper {
	private static final String PREFERENCES_NAME = "com.ykdl.member.kid.AccessTokenKeeper";
	private static final String PREFERENCES_NAME_ANONYMOUS = "com.ykdl.member.kid.AnonymousBean";
	private static  String HTTPTITLE_TOKEN= "";
	private static  String HTTPTITLE_TOKEN_TITLE= "";

	private String mAccessToken;
	private float mExpires_in;
	private String mRefresh_token;
	private String mActor_type;
	private String mActor_id;
	private Context mContext;
	public AccessTokenKeeper(Context context) {
		mContext = context;
		initAccessToken();
	}
	/**
	 * 初始化获取AccessToken
	 */
	private void initAccessToken() {
		if (mContext == null) {
			return;
		}
		if (KidConfig.IS_ANONYMOUS.equals(MyPrefs.getAnonymous(mContext))) {
			SharedPreferences pref = mContext.getSharedPreferences(PREFERENCES_NAME_ANONYMOUS, Context.MODE_APPEND);
			mAccessToken = pref.getString("anonymous_token", null);
			mRefresh_token = pref.getString("refresh_token", null);
			HTTPTITLE_TOKEN_TITLE = "anonymous_token";
		}else {
			SharedPreferences pref = mContext.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
			mAccessToken = pref.getString("access_token", null);
			mActor_id= pref.getString("actor_id", null);
			mActor_type = pref.getString("actor_type", null);
			mExpires_in = pref.getFloat("expires_in", 0);
			mRefresh_token = pref.getString("refresh_token", null);
			HTTPTITLE_TOKEN_TITLE = "access_token";
			long current = System.currentTimeMillis();
			long expries = (long)mExpires_in * 1000 ;
			if (current > expries) {
				clearAccessToken(mContext);
			}
		}
		
		}
	public String getmAccessToken() {
		return mAccessToken;
	}
	public String getMactor_id() {
		return mActor_id;
	}
	public String getRefresh_token(){
		return mRefresh_token;
	}
	public String getmAccessTokenTitle() {
		return HTTPTITLE_TOKEN_TITLE;
	}
	public boolean getmisAnonymous() {
		if (KidConfig.IS_ANONYMOUS.equals(MyPrefs.getAnonymous(mContext))) {
			return true;
		}else {
			return false;
		}
		
	}
	/**
	 * 保存accesstoken到SharedPreferences
	 * 
	 * @param context
	 *            Activity 上下文环境
	 * @param token
	 *            AnonymousBean
	 */
	public static void keepAnonymousToken(Context context, AnonymousBean token) {
		Log.e("alan", "--------keepAnonymousToken-------anonymous_id:"+token.getAnonymous_id());
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME_ANONYMOUS, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.putString("anonymous_id", token.getAnonymous_id());
		editor.putString("anonymous_token", token.getAnonymous_token());
		editor.putString("created_time", token.getCreated_time());
		editor.putString("device_id", token.getDisplay_name());
		HTTPTITLE_TOKEN = token.getAnonymous_token() ;
		HTTPTITLE_TOKEN_TITLE = "anonymous_token";
		editor.commit();
		MyPrefs.setAnonymous(context, KidConfig.IS_ANONYMOUS);
	}
	/**
	 * 从SharedPreferences读取anonymous_id
	 * 
	 * @param context
	 * @return AnonymousBean
	 */
	public static AnonymousBean readAnonymousToken(Context context) {
		AnonymousBean token = new AnonymousBean();
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME_ANONYMOUS, Context.MODE_APPEND);
		token.setAnonymous_id(pref.getString("anonymous_id", null));
		token.setAnonymous_token(pref.getString("anonymous_token", null));
		token.setCreated_time(pref.getString("created_time", null));
		token.setDisplay_name(pref.getString("device_id", null));
		return token;
	}
	
	/**
	 * 保存accesstoken到SharedPreferences
	 * 
	 * @param context
	 *            Activity 上下文环境
	 * @param token
	 *            RegisterPhonebean
	 */
	public static void keepAccessToken(Context context, RegisterPhonebean token) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.putString("access_token", token.getAccess_token());
		editor.putString("actor_id", token.getActor_id());
		editor.putString("actor_type", token.getActor_type());
		editor.putFloat("expires_in", token.getExpires_in());
		editor.putString("refresh_token", token.getRefresh_token());
		editor.commit();
		clearAnonymousToken(context);
		HTTPTITLE_TOKEN = token.getAccess_token() ;
		HTTPTITLE_TOKEN_TITLE = "access_token";
		MyPrefs.setAnonymous(context, KidConfig.NO_ANONYMOUS);
	}

	public static String getHTTPTITLE_TOKEN() {
		return HTTPTITLE_TOKEN;
	}
	/**
	 * 清空AnonymousToken
	 * 
	 * @param context
	 */
	public static void clearAnonymousToken(Context context) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME_ANONYMOUS, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.clear();
		editor.commit();
	}
	/**
	 * 清空AccessToken
	 * 
	 * @param context
	 */
	public void clearAccessToken(Context context) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.clear();
		editor.commit();
		this.mAccessToken = null;
		this.mRefresh_token = null;
		this.mExpires_in = 0;
		this.mActor_type =null;
		this.mActor_id =null;
	}

	public static void saveaccess_token(String access_token, Context con) {
		SharedPreferences preferences = con.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		preferences.edit().putString("access_token", access_token).commit();
	}

	public static String getaccess_token(Context con) {
		SharedPreferences preferences = con.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		return preferences.getString("access_token", null);
	}

	/**
	 * 从SharedPreferences读取access_token
	 * 
	 * @param context
	 * @return RegisterPhonebean
	 */
	public static RegisterPhonebean readAccessToken(Context context) {
		RegisterPhonebean token = new RegisterPhonebean();
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		token.setAccess_token(pref.getString("access_token", null));
		token.setActor_id(pref.getString("actor_id", null));
		token.setActor_type(pref.getString("actor_type", null));
		token.setExpires_in(pref.getFloat("expires_in", 0));
		token.setRefresh_token(pref.getString("refresh_token", null));
		return token;
	}
	/**
	 * 返回当前是否存储AccessToken
	 * 
	 * @return true-存在 false-不存在
	 */
	public boolean isAccessTokenExist() {
		if (mAccessToken == null) {
			return false;
		}
		return true;
	}


}
